from flask import Blueprint, jsonify, request
from db import db1
from utils.comm import get_data,send_message
from utils.rediscomm import r
from utils.jwt_token import myjwt
import json
import random
import time

bp_user = Blueprint('admin_user', __name__)


@bp_user.route('/rolesM', methods=['POST', 'GET'])
def rolesM():
    if request.method == 'POST':
        data = get_data()
        sql = """select id from roles where name ='%s'""" % (data['name'])
        res = db1.find_all(sql)
        if res:
            return jsonify({'code': 10010, "mes": '重复'})
        else:
            sql1 = """insert into roles values(0,'%s')""" % data['name']
            db1.insert(sql1)
            return jsonify({'code': 200})
    else:
        sql = """select * from roles"""
        res = db1.find_all(sql)
        return jsonify({'code': 200, "list": res})

@bp_user.route('/userM', methods=['POST', 'GET'])
def userM():
    if request.method == 'POST':
        data = get_data()
        sql = """select id from admin_user where name = '%s'""" % data['name']
        aa = db1.find_all(sql)
        if aa:
            return jsonify({"code": 10012})
        else:
            sql1 = """insert into admin_user values (0,'%s','%s',%d)""" % (
            data['name'], data['password'], data['role_id'])
            db1.insert(sql1)
            return jsonify({"code": 200})

@bp_user.route('/resourceM', methods=['POST', 'GET'])
def resourceM():
    if request.method == 'GET':
        sql = """select * from resources"""
        res = db1.find_all(sql)
        return jsonify({"code": 200, "list": res})
    elif request.method == 'POST':
        data = get_data()
        sql = """delete from roles_resource where roles_id = (%d)""" % int(data['resid'])
        db1.insert(sql)
        for i in data['rolesid']:
            sql1 = """insert into roles_resource values (0,%d,%d)""" % (int(data['resid']), i)
            db1.insert(sql1)
        return jsonify({"code": 200})


#管理员登录
#短信验证码
@bp_user.route('/sendsms', methods=['POST', 'GET'])
def sendsms():
    if request.method == 'POST':
        code = random.randint(1000,9999)
        mobile = request.args.get('mobile')
        value = r.get_str(mobile)
        if value:
            return jsonify({"code":10010,"mes":'请不要频繁发送'})
        flag = send_message(mobile,code,1)
        if flag:
            r.set_str(mobile,code,60)
            return jsonify({"code": 200})
        else:
            return jsonify({"code":10011,"mes":"发送失败，请重新发送"})


@bp_user.route('/yzmM',methods=['POST','GET'])
def yzmM():
    if request.method == 'POST':
        data = get_data()
        print(data)
        sql = """select * from admin_user where name=('%s')"""%data['mobile']
        ser = db1.find_one(sql)
        if ser:
            gcode = r.get_str(data['mobile'])
            if gcode:
                if int(gcode) == int(data['code']):
                    r.del_str(data['mobile'])
                    j = myjwt.jwt_encode(data)
                    d = myjwt.jwt_decode(j)
                    sqlq = """select * from roles_resource as re left join resources as es on re.resources_id=es.id where re.roles_id=(%d)"""%ser['role_id']
                    res = db1.find_all(sqlq)
                    return jsonify({"code":200,"j":j,"tlist":res,"ulist":ser,"d":d})
                else:
                    return jsonify({"code":10012,"mes":"验证码错误"})
            else:
                return jsonify({"code": 10012, "mes": "请发送验证码"})
        else:
            return jsonify({"code": 10013, "mes": "没有此用户"})









